# -*- encoding:utf-8 -*-

import requests

import re
from constant import constant_define
from service.base.Fetcher import Fetcher
import utils.CookieUtils as cu
__author__ = 'shudong.msd'

'''
<span class="stock-name"><div class="name">奥特迅</div><div class="price">SZ002227</div></span><span class="stock-weight weight">1.59%</span>
'''

class CubeFetcher(Fetcher):

    __headers = {
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
            'cache-control':'no-cache',
            'Connection':'keep-alive',
            'User-Agent': constant_define.user_agent,
            'Host':'xueqiu.com',
            'X-Requested-With':'XMLHttpRequest',
            'Cookie': cu.init_cookie()
    }

    def __init__(self,threads=0):
        Fetcher.__init__(self,threads)


    def set_cuTarget(self,target):
        self.__cuTarget = target


    # 返回{'symbol':symbol,'group':[(id,weight),()]}
    def do_work(self,req):
        content = requests.get(self.__cuTarget+req,headers=self.__headers).text
        re_str = r'<span class="stock-name"><div class="name">.+?</div><div class="price">(.*?)</div></span><span class="stock-weight weight">(.+?)%</span>'
        pattern = re.compile(re_str)
        res = pattern.findall(content)
        # print res
        return {'symbol':req,'group':res}
